 package game.gui;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import game.driver.Controller;
import game.model.Card;
import game.player.*;
import game.utils.Util;
import javax.swing.*;

/**
 * This class represents the window that pops up to see a player's cards of 
 * a certain color.
 * @author Kristen
 */
public class HandGUI extends JFrame implements ActionListener{
	
	private static final long serialVersionUID = 1119542407888189483L;
	JScrollPane scrollPane;
	JButton[] buttons;
	Object [] ocards;
	Card [] cards;
	Controller controller;
	
	/**
	 * This is the constructor for a window showing all the cards of the certain color.
	 * @param c, the controller
	 * @param p, the player
	 * @param color of cards to be looked at
	 */	
	public HandGUI(Controller c, Player p, String color){
		super(color+" "+Util.CARDS);
		controller=c;
		setSize(400, 200);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		addCards(p, color);
		setVisible(true);
		  }
	
	/**
	 * This method adds the cards in a scroll panel to the window
	 * @param p, the player
	 * @param c, the color of cards
	 */
	private void addCards(Player p, String c){
		JPanel cardPanel=new JPanel();
		
		if(c.equalsIgnoreCase(Util.GREEN)){
			ocards=p.getGreen().toArray();
	
		}
		if(c.equalsIgnoreCase(Util.RED))
			ocards=p.getRed().toArray();
		if(c.equalsIgnoreCase(Util.BLUE))
			ocards=p.getBlue().toArray();
		if(c.equalsIgnoreCase(Util.YELLOW))
			ocards=p.getYellow().toArray();
		if(c.equalsIgnoreCase(Util.WILD))
			ocards=p.getWild().toArray();

		buttons=new JButton[ocards.length];
		cards=new Card[ocards.length];
		
		for(int x=0;x<ocards.length;x++){
			if(ocards[x] instanceof Card)
				cards[x]=(Card) ocards[x];
			buttons[x] = new JButton((new ImageIcon(cards[x].getImageAddress())));
			cardPanel.add(buttons[x]);
			buttons[x].addActionListener(this);
		}
		
		scrollPane=new JScrollPane(cardPanel);	
		add(scrollPane);
	}
	/**
	 * This method recognizes when a card is selected and tells the controller
	 * to play that card
	 */
	public void actionPerformed(ActionEvent arg) {
		Object b=arg.getSource();
		if(b instanceof JButton){
			int x;
			for(x=0;x<buttons.length;x++){
				if(b.equals(buttons[x]))
					break;
			}
			dispose();
			controller.finishTurn(cards[x]);
		}
		
	 }
	
	}

